<template>
  <div style="display: inline-block;text-align: left;">
    <!-- 修改 - btn -->
    <el-button type="primary" icon="el-icon-edit" circle @click="dialogTableVisible = true"/>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" width="900px" title="修改" @close="close">
      <el-form ref="row_data" :model="row_data" :rules="rules" label-position="left" label-width="100px" style="width: 750px; margin-left:50px;">
        <el-form-item :label="'id'" prop="id">
          <el-input v-model="row_data.id" placeholder="id" disabled/>
        </el-form-item>
        <el-form-item :label="'商家或者用户ID'" prop="user_id">
          <el-input v-model="row_data.user_id" placeholder="商家或者用户ID"/>
        </el-form-item>
        <el-form-item :label="'用户类型'" prop="user_type">
          <el-select v-model="row_data.user_type" placeholder="请选择用户类型">
            <el-option v-for="item in select_user_type" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item :label="'意见内容'" prop="content">
          <tinymce :height="350" v-model="row_data.content" style="width:700px;" />
        </el-form-item>
        <el-form-item :label="'创建时间'" prop="create_time">
          <el-date-picker v-model="row_data.create_time" type="datetime" value-format="timestamp" placeholder="选择创建时间"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">编辑</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import store from '@/store'
import { edit } from '@/api/opinion/opinion'
import Tinymce from '@/components/Tinymce'

export default {
  name: 'DragDialogDemo',
  components: { Tinymce },
  directives: { elDragDialog },
  props: {
    row_data: {
      type: Object,
      default: function() {
        return null
      }
    }
  },
  data() {
    return {
      select_user_type: [],
      rules: {
        id: [
          { required: true, message: 'id不能为空', trigger: 'blur' }
        ],
        user_id: [
          { required: true, message: '商家或者用户ID不能为空', trigger: 'blur' }
        ],
        user_type: [
          { required: true, message: '用户类型不能为空', trigger: 'blur' }
        ],
        content: [
          { type: 'date', required: true, message: '请选择意见内容', trigger: 'blur' },
          { required: true, message: '意见内容不能为空', trigger: 'blur' }
        ],
        create_time: [
          { type: 'date', required: true, message: '请选择创建时间', trigger: 'blur' },
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false
    }
  },
  created: function() {
    console.log(this.row_data)
  },
  methods: {
    submitForm() {
      this.$refs.row_data.validate(valid => {
        if (valid) {
          const data = this.row_data
          data.create_time = parseInt(data.create_time / 1000)
          edit(store.getters.token, data).then(res => {
            if (res.status) {
              this.$message.success('修改成功！')
              this.close()
            } else {
              this.$message.error('修改失败！原因：' + res.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.$emit('getLists')
      this.dialogTableVisible = false
    }
  }
}
</script>
